#!/usr/bin/python3
# -*- coding: utf-8 -*-

import os
import re
from typing import Any, Dict, Literal

import allure
from playwright.sync_api import Page

from models.base_page import BasePage
from utils.log import log


class CreateRouterPage(BasePage):
    def __init__(self, page: Page):
        super().__init__(page)
        self.route = "vpc/create"

    @allure.step("选择可用区")
    def select_zone(self, zone_name: str = "") -> None:
        """选择可用区"""
        self.page.locator('.css-1ipurtk').click()
        if zone_name:
            self.page.get_by_role("button", name=zone_name).click()

    @allure.step("输入路由器名称")
    def fill_router_name(self, router_name: str = '') -> None:
        """输入路由器名称"""
        if router_name:
            self.page.get_by_role("textbox").nth(1).click()
            self.page.get_by_role("textbox").nth(1).fill('')
            self.page.locator("//div[@class='css-zg2fwz']//input[@class='css-114u1c2']").type(router_name)

    @allure.step("选择IPv6地址范围")
    def select_ipv6_address_range(self, ipv6_address_range: Literal['关闭 IPv6', '系统自动分配']) -> None:
        """选择IPv6地址范围"""
        self.page.locator("input[type='radio']+span").filter(has_text=f"{ipv6_address_range}").click()

    @allure.step("选择路由器类型")
    def select_router_type(self, router_type: Literal['免费型', '小型', '中型', '大型', '超大型'] = '小型') -> None:
        """选择路由器类型"""
        self.page.locator("input[type='radio']+span").filter(has_text=f"{router_type}").click()

    @allure.step("选择公网IP")
    def select_eip_type(self, eip_type: Literal['购买并绑定', '使用已有', '暂不绑定'] = '暂不绑定') -> None:
        """选择公网IP"""
        self.page.locator("input[type='radio']+span").filter(has_text=f"{eip_type}").click()

    @allure.step("输入私有网络名称")
    def fill_vxnet_name(self, vxnet_name: str = 'vxnet-e2e') -> None:
        """输入私有网络名称"""
        self.page.get_by_role("textbox").nth(2).click()
        self.page.get_by_role("textbox").nth(2).fill('')
        self.page.locator("//div[@class='css-13f0elt']//input[@class='css-114u1c2']").type(vxnet_name)

    @allure.step("点击立即提交按钮")
    def click_submit_now_button(self) -> None:
        """点击立即提交按钮"""
        self.toolbar.click_submit_now_button_by_role_button()

    def check_page_text_context(self) -> None:
        with allure.step("校验页面文本内容是否可见"):
            self.expect.to_be_visible(self.page.get_by_text("创建 VPC 网络"))
            self.expect.to_be_visible(self.page.get_by_text("基本信息"))
            self.expect.to_be_visible(self.page.get_by_text("初始私有网络"))
            self.expect.to_be_visible(self.page.get_by_text("建议选择靠近您客户的区域，可以降低网络时延，提高访问速度。"))

    def check_router_type_text_context(self, router_type: Literal['小型', '中型', '大型', '超大型', '免费型']) -> None:
        with allure.step("校验vpc网络类型的提示文案是否可见"):
            if router_type == "小型":
                self.expect.to_be_visible(self.page.get_by_text("管理流量转发能力:100 kpps"))
            elif router_type == "中型":
                self.expect.to_be_visible(self.page.get_by_text("管理流量转发能力:150 kpps"))
            elif router_type == "大型":
                self.expect.to_be_visible(self.page.get_by_text("管理流量转发能力:200 kpps"))
            elif router_type == "超大型":
                self.expect.to_be_visible(self.page.get_by_text("管理流量转发能力:250 kpps"))
            elif router_type == "免费型":
                self.expect.to_be_visible(self.page.get_by_text("仅具有内网通信功能，不支持绑定公网IP和转发公网流量"))
            else:
                log.error(f"{router_type}参数错误")
